package com.zone.service.service.impl;

import java.util.List;

import com.zone.common.core.constant.UserConstants;
import com.zone.common.core.utils.DateUtils;
import com.zone.common.core.utils.StringUtils;
import com.zone.system.api.domain.SysDept;
import io.lettuce.core.dynamic.annotation.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.zone.service.mapper.EmployeesMapper;
import com.zone.service.domain.Employees;
import com.zone.service.service.IEmployeesService;

/**
 * 人员管理Service业务层处理
 *
 * @author liuyang
 * @date 2025-10-21
 */
@Service
public class EmployeesServiceImpl implements IEmployeesService {
    @Autowired
    private EmployeesMapper employeesMapper;

    /**
     * 查询人员管理
     *
     * @param id 人员管理主键
     * @return 人员管理
     */
    @Override
    public Employees selectEmployeesById(Long id) {
        return employeesMapper.selectEmployeesById(id);
    }

    /**
     * 查询人员管理列表
     *
     * @param employees 人员管理
     * @return 人员管理
     */
    @Override
    public List<Employees> selectEmployeesList(Employees employees) {
        return employeesMapper.selectEmployeesList(employees);
    }

    /**
     * 新增人员管理
     *
     * @param employees 人员管理
     * @return 结果
     */
    @Override
    public int insertEmployees(Employees employees) {
        employees.setCreateTime(DateUtils.getNowDate());
        try {
            return employeesMapper.insertEmployees(employees);
        } catch (Exception e) {
            // 检查是否是唯一约束冲突异常
            if (e.getMessage().contains("Duplicate entry")) {
                throw new RuntimeException("员工工号已存在");
            }
            throw e;
        }
    }

    /**
     * 修改人员管理
     *
     * @param employees 人员管理
     * @return 结果
     */
    @Override
    public int updateEmployees(Employees employees) {
        employees.setUpdateTime(DateUtils.getNowDate());
        return employeesMapper.updateEmployees(employees);
    }

    /**
     * 批量删除人员管理
     *
     * @param ids 需要删除的人员管理主键
     * @return 结果
     */
    @Override
    public int deleteEmployeesByIds(Long[] ids) {
        return employeesMapper.deleteEmployeesByIds(ids);
    }

    /**
     * 删除人员管理信息
     *
     * @param id 人员管理主键
     * @return 结果
     */
    @Override
    public int deleteEmployeesById(Long id) {
        return employeesMapper.deleteEmployeesById(id);
    }

    /**
     * 批量插入人员数据
     *
     * @param employeesList
     * @return 结果
     */
    @Override
    public int batchInsertEmployees(List<Employees> employeesList) {
        try {
            return employeesMapper.batchInsertEmployees(employeesList);
        } catch (Exception e) {
            // 检查是否是唯一约束冲突异常
            if (e.getMessage().contains("Duplicate entry")) {
                throw new RuntimeException("员工工号已存在");
            }
            throw e;
        }
    }

    /**
     * 校验员工编号是否唯一
     *
     * @param employees 员工信息
     * @return 结果
     */
    @Override
    public boolean checkEmployeeNoUnique(Employees employees) {
        Long id = StringUtils.isNull(employees.getId()) ? -1L : employees.getId();
        Employees info = employeesMapper.checkEmployeeNoUnique(employees.getEmpNo());
        if (StringUtils.isNotNull(info) && info.getId().longValue() != id.longValue()) {
            return UserConstants.NOT_UNIQUE;
        }
        return UserConstants.UNIQUE;
    }
}
